#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Create on May 2011

@author Wang Qiang
"""


import sys
import os
path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(path)

from QQWeiboAPI.qqAPI import QQAPI
from QQWeiboAPI import oauth
import unittest
import simplejson as json


class TTest(unittest.TestCase):
    """
    Test for the t operation
    t/show
    t/add
    t/del
    t/re_add
    t/reply
    t/add_pic
    t/re_count
    t/re_list
    t/comment
    t/add_video
    t/add_music
    t/getvideoinfo
    """

    consumer_key = ''
    consumer_secret = ''
    access_key = ''
    access_secret = ''
            
    def testConsumer(self):
        """test for loading consumer"""
        path = os.path.join(os.path.dirname(__file__), 'consumer.txt')
        f = open(path, 'r')
        consumerStr = f.read()
        f.close()
        consumer = json.loads(consumerStr)
        self.consumer_key = consumer['key']
        self.consumer_secret = consumer['secret']
        self.assertEqual(len(self.consumer_key), 32)
        self.assertEqual(len(self.consumer_secret), 32)
   
    def testAccessToken(self):
        """test for loading access_token"""
        path = os.path.join(os.path.dirname(__file__), 'access_token.txt')
        f = open(path, 'r')
        access_tokenStr = f.read()
        f.close()
        access_token = json.loads(access_tokenStr)
        self.access_key = access_token['key']
        self.access_secret = access_token['secret']
        self.assertEqual(len(self.access_key), 32)
        self.assertEqual(len(self.access_secret), 32)
    
    def testTShow(self):
        """test for t/show"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.show_t(format='json', id=22099046880488)
        print res.msg
        self.assertEqual(res.ret, 0)
        print res.data.text

    def testTAdd(self):
        """test for t/add"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        message = "python unit test of t/add. 测试信息!"
        res = testAPI.add_t(format='json', content=message)
        self.assertEqual(res.ret, 0)

    def testTDel(self):
        """test for t/del"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.del_t(format='json', id=22100068615557)
        self.assertEqual(res.ret, 0)
    
    def testTReAdd(self):
        """test for t/re_add"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        message = "转发一条微博!"
        res = testAPI.re_add_t(format='json', content=message, reid=13100011055628)
        self.assertEqual(res.ret, 0)

    def testTReply(self):
        """test for t/reply"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        message = "test qq python api reply_t, 微博回复测试!(初学者学python)"
        res = testAPI.reply_t(format='json', content=message, reid=13100011055628)
        self.assertEqual(res.ret, 0)

    def testTReply(self):
        """test for t/reply"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        message = "test qq python api reply_t, 微博回复测试!(初学者学python)"
        res = testAPI.reply_t(format='json', content=message, reid=13100011055628)
        self.assertEqual(res.ret, 0)
    
    def testTAddPic(self):
        """test for t/add_pic"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        message = "test for python qq api, 测试发送一张图片"
        picFilename = os.path.join(os.path.dirname(__file__), 'soy.jpg')
        res = testAPI.add_pic_t(format='json', content=message, pic=picFilename)
        self.assertEqual(res.ret, 0)
        
    def testTReCount(self):
        """test for t/re_count"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        weibolist = 13100011055628
        res = testAPI.re_count_t(format='json', ids=weibolist, flag=0)
        self.assertEqual(res.ret, 0)
        res = testAPI.re_count_t(format='json', ids=weibolist, flag=1)
        self.assertEqual(res.ret, 0)
        res = testAPI.re_count_t(format='json', ids=weibolist, flag=2)
        self.assertEqual(res.ret, 0)

    def testTReList(self):
        """test for t/re_list"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        weiboid = 13100011055628
        res = testAPI.re_list_t(format='json', rootid=weiboid, flag=0, \
                                 pageflag=0, pagetime=0, reqnum=20, \
                                 twitterid=0)
        self.assertEqual(res.ret, 0)
        for info in res.data.info:
            print info.text
    
    def testTComment(self):
        """test for t/comment"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        message = "test qq python api coment_t, 微博点评测试!(初学者学python)"
        res = testAPI.comment_t(format='json', content=message, reid=13100011055628)
        self.assertEqual(res.ret, 0)
    
    def testTAddMusic(self):
        """test for t/add_music"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        message = "test qq python api add_music 音乐微博测试!(初学者学python)"
	musicUrl='http://www.chzhu.cn/mymusic/%d0%c5%d1%f6.mp3'
        res = testAPI.add_music_t(format='json', content=message, \
                                  url=musicUrl, title='信仰', author='张信哲')
        self.assertEqual(res.ret, 0)
    
    def testTAddVideo(self):
        """test for t/add_video"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        message = "test qq python api add_video 视频微博测试!(初学者学python)"
	videoUrl='http://www.tudou.com/programs/view/old7Lx7KlSw/'
        res = testAPI.add_video_t(format='json', content=message, \
                                  url=videoUrl)
        self.assertEqual(res.ret, 0)

    def testTGetvideoinfo(self):
        """test for t/getvideoinfo"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
	videoUrl='http://www.tudou.com/programs/view/old7Lx7KlSw/'
        res = testAPI.getvideoinfo_t(format='json', url=videoUrl)
        self.assertEqual(res.ret, 0)
        print res.data.player
        print res.data.real
        print res.data.title


if __name__ == '__main__':
    #unittest.main()
    fast = unittest.TestSuite()
    fast.addTest(TTest('testTGetvideoinfo'))
    unittest.TextTestRunner().run(fast)
